<?php
/**
 * +-------------------------------------------------------------
 * |Caption:
 * +-------------------------------------------------------------
 * |Author: Lenny
 * +-------------------------------------------------------------
 * |Email:  1685964109@qq.com
 * +-------------------------------------------------------------
 * |Date:   2024/7/3 下午2:30
 * +-------------------------------------------------------------
 */

namespace app\admin\service;

class CategoryService
{
    /**
     * 递归获取树形结构
     * @param     $list
     * @param int $pid
     * @return array
     */
    public function getTreeData($list, int $pid = 0): array
    {
        $data = [];
        foreach($list as $key => $item) {
            if($item['pid'] == $pid) {
                $children = $this->getTreeData($list, $item['id']);
                !empty($children) && $item['children'] = $children;
                $data[] = $item;
                unset($list[$key]);
            }
        }
        return $data;
    }
}